﻿using System.Text;
using System.Windows;

namespace zaklad_rtv
{
    /// <summary>
    /// Interaction logic for ClientForm.xaml
    /// </summary>
    public partial class ClientForm
    {
        public ClientForm()
        {
            InitializeComponent();
        }

        protected override void FillFormFromObject()
        {
            MainWindow._clientFormTab.Header = "Edytuj klienta";
            var client = (Client)EditObject;
            _tbFirstName.Text = client.FirstName;
            _tbLastName.Text = client.LastName;
            _tbTelephone.Text = client.Telephone;
            _tbAddress.Text = client.Address;
        }

        protected override void ResetForm()
        {
            MainWindow._clientFormTab.Header = "Nowy klient";
            _tbAddress.Text = "";
            _tbFirstName.Text = "";
            _tbLastName.Text = "";
            _tbTelephone.Text = "";
        }

        private void ClearForm(object sender, RoutedEventArgs e)
        {
            EditObject = null;
        }

        private void SaveForm(object sender, RoutedEventArgs e)
        {
            if (ValidateForm())
            {
                var client = new Client
                {
                    Address = _tbAddress.Text,
                    FirstName = _tbFirstName.Text,
                    LastName = _tbLastName.Text,
                    Telephone = _tbTelephone.Text
                };
                if (EditObject != null && EditObject.Id > 0)
                {
                    client.Id = EditObject.Id;
                    MainWindow.DBHelper.Update(client);
                    RaiseEvent(new DatabaseObjectChangedEventArgs(AppEvents.DatabaseObjectChangedEvent, sender, client, EventAction.Update));
                    MessageBox.Show("Pomyślnie edytowano klienta!", "Sukces", MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    EditObject = null;
                }
                else
                {
                    MainWindow.DBHelper.Save(client);
                    RaiseEvent(new DatabaseObjectChangedEventArgs(AppEvents.DatabaseObjectChangedEvent, sender, client, EventAction.Insert));
                    MessageBox.Show("Pomyślnie dodano klienta!", "Sukces", MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    EditObject = null;
                }
                MainWindow.ChangeActiveTab(MainWindow.Tabs.ClientList);
            }
        }

        protected override bool ValidateForm()
        {
            var errors = new StringBuilder();
            var validationResult = true;
            if (string.IsNullOrEmpty(_tbFirstName.Text.Trim()))
            {
                errors.AppendLine("Należy podać imię klienta.");
                validationResult = false;
            }
            if (string.IsNullOrEmpty(_tbLastName.Text.Trim()))
            {
                errors.AppendLine("Należy podać nazwisko klienta.");
                validationResult = false;
            }
            var errorString = errors.ToString();
            if (!string.IsNullOrEmpty(errorString))
            {
                MessageBox.Show(errorString, "Błąd", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return validationResult;
        }
    }

}
